using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Web.CSH;
using Ent.Web.WebControl;
using Ent.Voice.Show;
using Ent.Voice.Nomination;
using Ent.Web.Helper;
using System.IO;
public partial class Controls_Updates : System.Web.UI.UserControl
{
    private int _catId = -1;
    public int CatagoryId
    {
        get { return _catId; }
        set { _catId = value; }
    }
    private Unit _height = Unit.Percentage(100);
    public Unit Height
    {
        get { return _height; }
        set { _height = value; }
    }
    private Unit _width = Unit.Percentage(100);
    public Unit Width
    {
        get { return _width; }
        set { _width = value; }
    }
    public Controls_Updates()
    {
        this.PreRender += new EventHandler(Controls_Updates_PreRender);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    void Controls_Updates_PreRender(object sender, EventArgs e)
    {
        tcLeft.Width = _width;
        tcLeft.Height = _height;
        Image img = new Image();
        img.ID = "imgNews";
        img.AlternateText = "News";
        img.ImageUrl = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/news.gif";
        tcLeft.FirstTabControl = img;
        img = new Image();
        img.ID = "imgHelp";
        img.AlternateText = "Help";
        img.ImageUrl = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/help.gif";
        tcLeft.ThirdTabControl = img;
        img = new Image();
        img.ID = "imgWinners";
        img.AlternateText = "Winners";
        img.ImageUrl = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/winners.gif";
        tcLeft.SecondTabControl = img;
        iControls_Marque cm = (iControls_Marque)LoadControl("../Controls/Marque.ascx");

        //Latest Event
        Event[] evs = Event.GetLatestTopNEvent(10, _catId);
        cm.MarqueText = Common.SerializeForUpdate(evs);
        tcLeft.FirstPanelControl = (System.Web.UI.UserControl)cm;

        //Help
        string Language = Session["LANG"] == null ? "en-US" : Session["LANG"].ToString();
        HelpDocument hd = new HelpDocument(Language, Server.MapPath("~"));
        string help = hd.GetSummary(Request.Url.Segments[Request.Url.Segments.Length - 1]);
        Label lbl = new Label();
        lbl.Text = "<table class='general' cellpadding='0' cellspacing='0'><tr><td align='center'>";
        lbl.Text += "<img src='" + ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/smile.gif'/>";
        lbl.Text += "</td></tr><tr><td align='center'>";
        lbl.Text += help;
        lbl.Text += "</td></tr><tr><td align='right'>";
        lbl.Text += "<a style='cursor: pointer;' onclick=\"window.open('" + ConfigurationManager.AppSettings["BaseUrl"] + "/Help.aspx?id=" + Request.Url.Segments[Request.Url.Segments.Length - 1] + "','name','height=400,width=550,scrollbars=yes,resizable=0,maximize=0');\"><font style=\"font-size:small\" color='#5393da'>" + ResourceHelper.Get("LBL_More") + "</font></a>";
        lbl.Text += "</td></tr></table>";
        tcLeft.ThirdPanelControl = lbl;

        //Winners
        cm = (iControls_Marque)LoadControl("../Controls/Marque.ascx");
        evs = Event.GetTopNLatestResults(10, _catId, 2);
        WebControl wCtrl = SetWinnerControl(evs);
        if (wCtrl != null)
            cm.MarqueControl = wCtrl;
        tcLeft.SecondPanelControl = (System.Web.UI.UserControl)cm;
    }
    
    private WebControl SetWinnerControl(Event[] evs)
    {
        int maxForEach = 2;
        Table tbl = new Table();
        if (evs == null) return null;
        for (int i = 0; i < evs.Length; i++)
        {
            Event e = (Event)evs.GetValue(i);
            if (e == null)
                continue;
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tbl.CssClass = "general text10";
            tbl.Width = Unit.Percentage(100);
            tbl.CellPadding = 0;
            tbl.CellSpacing = 0;
            tc.ColumnSpan = 2;
            tc.Style.Add(HtmlTextWriterStyle.PaddingTop, "2px");
            tc.Height = Unit.Pixel(20);
            tc.Text = "<div class='text11 header1 cover' valign='middle' align='left'>" + Common.GetImageTextLink("ShowEvent.aspx?id=" + e.Id,"Images/dj/event.gif",StringHelper.RestrictMaxChar(e.Name,15),15) + "</div>";
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
            for (int j = 0; j < e.Winners.Length; j++)
            {
                if (j >= maxForEach)
                    break;
                string imgLoc;
                if (j < 2)
                    imgLoc = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/trophy" + Convert.ToString(j + 1) + ".gif";
                else
                    imgLoc = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/trophy.gif";
                tr = new TableRow();
                tc = new TableCell();
                tc.Width = Unit.Percentage(25);
                tc.HorizontalAlign = HorizontalAlign.Center;
                tc.VerticalAlign = VerticalAlign.Middle;
                tc.Text = "<img src='" + imgLoc + "' style='width: 20px; height: 30px;'/>";
                tr.Cells.Add(tc);
                tc = new TableCell();
                tc.HorizontalAlign = HorizontalAlign.Center;
                tc.Width = Unit.Percentage(75);
                tc.Style.Add(HtmlTextWriterStyle.PaddingTop, "2px");
                tc.Style.Add(HtmlTextWriterStyle.PaddingRight, "2px");
                DashEntry de = (DashEntry)CreateDashEntry(((EventWinner)e.Winners.GetValue(j)).Entry);
                tc.Controls.Add(de);
                tr.Cells.Add(tc);
                tbl.Rows.Add(tr);
            }
        }
        return tbl;
    }
    private iControls_DashEntry CreateDashEntry(Entry entry)
    {
        iControls_DashEntry de = (iControls_DashEntry)LoadControl("DashEntry.ascx");
        Common.SetDashEntry(ref de, tcLeft.EntryTemplate, 1);
        de.Entry = entry;
        return de;
    }
}
